package com.cetc.nsfx.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cetc.nsfx.security.vo.UserContext;
import com.cetc.nsfx.service.sys.RoleService;

@Controller
@RequestMapping("/sys/role")
public class RoleController {
	
	@Autowired
	RoleService userRoleService;
	
	
	@RequestMapping("/toRoleList")
	public String toRoleList(){
		return "/sys/roleList";
	}
	
	/**
	 * 组织人员信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request,String T_CODE) {
		
		String nodeid = T_CODE;
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = userRoleService.loadUserRightGrid(nodeid,user);
		reValue.put("aaData",pageData);
		return reValue;
	}
	
	/**
	 * 岗位信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "rolelist",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> rolelist(HttpServletRequest request) {

		String loginName = request.getParameter("loginName");//选中人员，对应税务人员工号
		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = new ArrayList<Map<String,Object>>();
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		if(!"".equals(loginName)){//查看
			pageData = userRoleService.getUserRolelist(loginName,user);
		}else{//设置
//			pageData = userRightService.getRolelist(user);
		}
		reValue.put("aaData",pageData);
		return reValue;
	}
	
	/**
	 * 保存人员授权信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saveUserRole(HttpServletRequest request) {
		
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map=new HashMap<String,Object>();
		String stationID = request.getParameter("stationID");//角色
		String userLoginName = request.getParameter("userLoginName");//用户
		int result= userRoleService.saveUserRole(stationID,userLoginName,user.getUserId());
		if(result==1){
			map.put("success", "success");
		}else{
			map.put("success", "failed");
		}
		return map;
	}
	
	/**
	 * 根据用户查询角色具有的菜单权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "roleMenuData",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> roleMenuData(HttpServletRequest request) {

		String principalName = request.getParameter("principalName");
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = new ArrayList<Map<String,Object>>();
		pageData = userRoleService.getRoleMenulist(principalName,user.getUserId());
		reValue.put("aaData",pageData);
		return reValue;
	}
	
	/**
	 * 用户权限菜单
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "userRoleData",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> userRoleData(HttpServletRequest request) {

		String principalName = request.getParameter("principalName");
		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = new ArrayList<Map<String,Object>>();
		pageData = userRoleService.getUserRoleList(principalName);
		reValue.put("aaData",pageData);
		return reValue;
	}
	
	/**
	 * 根据菜单编码查询下级菜单（用户权限）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/loadMenuData",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> loadMenuData(HttpServletRequest request) {

		String parentMenuCode = request.getParameter("parentMenuCode");//该值为空则查询用户权限的所有菜单
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = new ArrayList<Map<String,Object>>();
		
		Map<String,String> param = new HashMap<String,String>();
		param.put("code", parentMenuCode);
		param.put("loginName", user.getUserId());
		pageData = userRoleService.getChildMenuList(param);
		//request.setAttribute(request.getParameter("menutype").toString(),true);
		reValue.put("aaData",pageData);
		return reValue;
	}
	
	/**
	 * 根据当前用户权限查询制定用户岗位
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "userStationData",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> userStationData(HttpServletRequest request) {

		String principalName = request.getParameter("principalName");
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		Map<String, Object> reValue = new HashMap<String, Object>();
		List<Map<String,Object>> pageData = new ArrayList<Map<String,Object>>();
		pageData = userRoleService.getUserStationList(principalName,user);
		reValue.put("aaData",pageData);
		return reValue;
	}
	
	/**
	 * 根据传入菜单编码查询当前用户是否有权限
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/userMenu",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> userMenu(HttpServletRequest request) {

		String menuCode = request.getParameter("menuCode");
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		Map<String, Object> reValue = new HashMap<String, Object>();
		int count = userRoleService.getUserMenuByMenuCode(menuCode,user.getUserId());//0:没有菜单权限
		reValue.put("count",count);
		return reValue;
	}
}
